
//
//  HToriginalStatusView.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/21.
//  Copyright © 2016年 liujonson. All rights reserved.
//

let lableFont : CGFloat = 14
let sourceFont : CGFloat = 10
import UIKit
import SnapKit
class HToriginalStatusView: UIView {
    
    
    var bottomCons : Constraint?
    
    var statusViewModel : HTStatsCellViewModel? {
    
        didSet{
        
        headImage.sd_setImageWithURL(NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named : "avatar_default_small"))
            
        nameLbale.text = statusViewModel?.status?.user?.name
            
        vipImage.image = statusViewModel?.memberImage
            
        timeLbale.text = statusViewModel?.createTime
            
        sourceLable.text = statusViewModel?.sourceText
            
        contentLable.text = statusViewModel?.status?.text
            
        bottomCons?.uninstall()
            if  let  pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
            
//                pictureView.pic_urls = pic_urls
//                
//                pictureView.snp_makeConstraints(closure: { (make) -> Void in
//                    make.size.equalTo(statusViewModel?.originalPictureViewSize ?? CGSizeZero)
//                })
                
                

                pictureView.dataInfo = (pic_urls,statusViewModel!.originalPictureViewSize)
                //显示控件
                pictureView.hidden = false
                
                //更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
                })
            }else{
            
            
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(contentLable).offset(HMStatusCellMargin).constraint
                })
            }
            
            
            
        }
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
//        backgroundColor = UIColor(red: CGFloat(random()%256)/255, green: CGFloat(random()%256)/255, blue: CGFloat(random()%256)/255, alpha: 1)
        
     //设置约束
        addSubview(vipImage)
        addSubview(headImage)
        addSubview(nameLbale)
        addSubview(timeLbale)
        addSubview(sourceLable)
        addSubview(contentLable)
        addSubview(verifiedImageView)
        addSubview(pictureView)

        headImage.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: HTStatusCellBottomViewH, height: HTStatusCellBottomViewH))
            
        }
        
        nameLbale.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImage)
            make.leading.equalTo(headImage.snp_trailing).offset(HMStatusCellMargin)
            
        }
        
        //会员约束
        vipImage.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(nameLbale)
            make.leading.equalTo(nameLbale.snp_trailing).offset(HMStatusCellMargin)
        }
        
        timeLbale.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(headImage)
            make.leading.equalTo(nameLbale)
        }
        
        //来源约束
        sourceLable.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLbale.snp_trailing).offset(HMStatusCellMargin)
            make.top.equalTo(timeLbale)
        }
        
        contentLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImage)
            make.top.equalTo(headImage.snp_bottom).offset(8)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_trailing).offset(-2)
            make.centerY.equalTo(headImage.snp_bottom).offset(-2)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLable)
            make.top.equalTo(contentLable.snp_bottom).offset(HMStatusCellMargin)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            bottomCons =  make.bottom.equalTo(pictureView.snp_bottom).offset(HMStatusCellMargin).constraint
        }
    }

    
    
    //头像
    private lazy var headImage : UIImageView = UIImageView(image: UIImage(named: "avatar_default_small"))
    
    //昵称
    private lazy var nameLbale : UILabel = {
    
        let lable = UILabel()
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(lableFont)
        lable.text = "hah"
        return lable
    }()
    
    //设置会员图片
    private lazy var vipImage : UIImageView = UIImageView (image: UIImage(named: "common_icon_membership"))
    
    //设置时间
    private lazy var timeLbale : UILabel = {
    
        let lable = UILabel()
        lable.text = "杠杆"
        lable.textColor = UIColor.orangeColor()
        lable.font = UIFont.systemFontOfSize(sourceFont)
        
        return lable
    }()
    
    //设置来源
    private lazy var sourceLable : UILabel = {
    
        let lable = UILabel()
        lable.text = "iPhone6s"
        lable.textColor = UIColor.blueColor()
        lable.font = UIFont.systemFontOfSize(sourceFont)
        
        return lable
    }()
    
    //认证
    private lazy var verifiedImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //微博正文
    private lazy var contentLable : UILabel = {
    
       let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(HTstatusCellFont)
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 2 * HMStatusCellMargin
        label.numberOfLines = 0
        return label
    }()
    
    
    private lazy var pictureView : HTStatusPictrueView = HTStatusPictrueView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())


    
    
    
    
    
}
